package frame.system.web.auth;

import frame.base.Ajax;
import frame.base.BaseWeb;
import frame.base.BootstrapTable;
import frame.system.qo.AuthQo;
import frame.system.service.DeptService;
import frame.system.to.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping(DeptWeb.DIR)
public class DeptWeb extends BaseWeb{

    public static final String DIR = "/web/system/auth/dept";

    @Autowired
    private DeptService deptService;


    @RequestMapping("/index")
    public String index(Model model, AuthQo qo) {
        model.addAttribute("dir", DIR);
        model.addAttribute("qo", qo);
        return "system/auth/deptlist";
    }
    @RequestMapping("/table")
    @ResponseBody
    public BootstrapTable<Dept> table(AuthQo qo) {
        return deptService.pageList(qo);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Ajax add(@Valid() Dept dept, BindingResult bd) {
        try {
            super.checkFormData(bd);
            deptService.addSelective(dept);
            return Ajax.success(dept,"数据创建成功！");
        } catch (Exception e) {
            return Ajax.faild("数据创建失败：" + e.getMessage());
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Ajax delete(String[] ids) {
        try {
            List<Integer> idList = strId2Integer(ids);
            int re =  deptService.remove(idList);
            return Ajax.success(re, "数据删除成功");
        } catch (Exception e) {
            return Ajax.faild("数据删除失败：" + e.getMessage());
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public Ajax update(@Valid() Dept dept, BindingResult bd) {
        try {
            checkFormData(bd);
            int re = deptService.updateSelective(dept);
            return Ajax.success(re,"数据更新成功！");
        } catch (Exception e) {
            return Ajax.faild("数据更新失败：" + e.getMessage());
        }
    }




    @RequestMapping("/get")
    @ResponseBody
    public Ajax get(String id) {
        try {
            Integer id0 = strId2Integer(id);
            Dept dept = deptService.selectByPrimaryKey(id0);
            return Ajax.success(dept,"数据查询成功");
        } catch (Exception e) {
            return Ajax.faild("数据查询失败：" + e.getMessage());
        }
    }

}
